﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserManage
    {
        private static readonly UserDal Dal = new UserDal();


        public void Delete(int userId)
        {
            Dal.Delete(userId);
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public User GetInfo(int userId)
        {
            return Dal.GetInfo(userId);
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public User GetInfo(string userName)
        {
            return Dal.GetInfo(userName);
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="hasRights"></param>
        /// <returns></returns>
        public User GetInfo(string userName, bool hasRights)
        {
            return Dal.GetInfo(userName, true);
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="isAdmin"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<User> BindList(bool isAdmin, PagingParam pp)
        {
            return new UserDal().BindList(isAdmin, pp);
        }

        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static UserLoginInfo Current
        {
            get
            {
                HttpContext current = HttpContext.Current;
                if (current == null)
                {
                    throw new Exception("HttpContext.Current 为空值。");
                }
                if (!current.User.Identity.IsAuthenticated)
                {
                    return null;
                }
                if (HttpContext.Current.Session["User"] != null)
                {
                    return HttpContext.Current.Session["User"] as UserLoginInfo;
                }
                User user = Dal.GetInfo(current.User.Identity.Name, true);

                var uli = new UserLoginInfo
                {
                    UserID = user.Id,
                    UserName = user.UserName,
                    RightCodes = user.Rights.Select(x => x.ModelId + x.Code).ToList(),
                    Rank = Dal.GetMaxRank(user.Id)
                };
                HttpContext.Current.Session["User"] = uli;

                return uli;
            }
        }
    }
}